<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>首页</title>
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css"/>
    <script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="bower_components/angular/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module("app", []);
        app.controller("sqlparser", ["$scope", function (scope) {
            scope.sql = "select 1 from dual";
            scope.cond = "";
            scope.paramList = [];
            scope.sqlRst = "";
			scope.nameStyle="named";

            /**
             * 解析查询语句
             * @param sql
             */
            scope.parseSql = function () {
                var tmpSql = scope.sql.replace(/\n/g, "");
                angular.forEach(scope.paramList, function (p) {
                    tmpSql = tmpSql.replace(new RegExp(":" + p.key + "<.+?>"), p.val);
                });
                scope.sqlRst = tmpSql;
            };

            /**
             * 解析条件
             * @param cond
             */
            scope.parseCond = function () {
                var pattern = /\:(.+?)\=(.+?)\s/g;
				var idxPat= /(\w+?)\=(.+?)\s/g;  //序列模式reg
                var rst;
                var paramMap = {};
                //补个空格，防止丢失最后一个参数
                var tmpStr = scope.cond.replace(/\n/g, "").replace(/\"/g,"'") + " ";
                tmpStr=convertDate(tmpStr);
				console.log(tmpStr);

                scope.paramList =[];
				if(scope.nameStyle === 'named'){
					while ((rst = pattern.exec(tmpStr)) !== null) {
						scope.paramList.push({'key': rst[1], 'val': rst[2].replace(/\"/g, "'")});
					}
				}else if(scope.nameStyle === 'order'){
					var idx=0; //序列模式
					while ((rst = idxPat.exec(tmpStr)) !== null) {
					    if(rst[1].startsWith("_")) continue; //跳过下划线开头的变量
						scope.paramList.push({'key': idx++, 'val': rst[2].replace(/\"/g, "'")});
					}
				}
                
                scope.parseSql();
            };

            /**
             * 转化条件里头包含日期的字段
             * 防止将 selecting condition = :owc0=2015-08-08 09:25:18 中的 :25:18解析成字段名
             * @param sql
             * @returns {string}
             */
            var convertDate=function(sql){
                var pattern =/\d{4}\-\d{1,2}\-\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}/g;
                var rst;
                var timeStr;
                while ((rst = pattern.exec(sql)) !== null) {
                    //将日期格式修改成 YYYY-MM-DD-HH24-MI-SS
                    timeStr =rst[0].replace(/[\s\:]/g,"-");
                    sql = sql.replace(rst[0],"to_date('"+timeStr+"','YYYY-MM-DD-HH24-MI-SS')");
                    pattern.lastIndex=0;
                }
                return sql;
            }


        }]);
    </script>
</head>

<body ng-app="app">
<div class="container-fluid" ng-controller="sqlparser">
    <h1>SQL Parser</h1>

    <div class="row">
        <div class="col-sm-6">
            <form>
			<div class="form-group">
                    <lable class="control-label">绑定方式</lable>
					             <lable class="radio-inline"><input type="radio" ng-model="nameStyle" value="named" ng-change="parseCond()"/>命名</lable>
					             <lable class="radio-inline"><input type="radio" ng-model="nameStyle" value="order" ng-change="parseCond()" />序列</lable>

                    
                </div>
                <div class="form-group">
                    <lable class="control-label">查询语句</lable>
                    <textarea class="form-control" ng-model="sql" cols="30" rows="20"
                              ng-change="parseSql()"> </textarea>
                </div>
                <div class="form-group">
                    <lable class="control-label">查询条件</lable>
                    <textarea class="form-control" ng-model="cond" cols="30" rows="20"
                              ng-change="parseCond()"> </textarea>
                </div>
                <div class="form-group">
                    <lable class="control-label">结果</lable>
                    <textarea class="form-control" ng-model="sqlRst" cols="30" rows="20"> </textarea>
                </div>

            </form>
        </div>
        <div class="col-sm-6">
            <div class="panel">
                <h3 class="panel-heading">参数列表</h3>
                <ul class="list-group">
                    <li class="list-group-item" ng-repeat="p in paramList">
                        <lable class="label label-info ">{{p.key}}</lable>
                        <input ng-model="p.val" ng-change="parseSql()"/>
                    </li>
                </ul>
            </div>

        </div>
    </div>
</div>
<footer>
    powered by <a href="http://git.oschina.net/rikugun"> rikugun</a>
</footer>
</body>

</html>